
/*
* Copyright 2010, Center for Advanced Information Systems,Nanyang Technological University
*
* File name: InfoChart.java
*
* Abstract: Construct charts for the prequery time and current candidate size
* Current Version:      0.1
* Auther:               Jin Changjiu
* Modified Date:        Jun.3,2010
*
*/
package gblend.result;

import java.awt.Font;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.RefineryUtilities;

public class InfoChart extends JFrame {

    /**
     * Creates a new demo instance.
     *
     * @param title  the frame title.
     */
    public InfoChart(String title) {
        super(title);
     
    }

     public void set(Vector<Record> dataRecord) {

        JPanel chartPanel = createDemoPanel(dataRecord);
        chartPanel.setVisible(true);
        setContentPane(chartPanel);
    }


     public static JFreeChart createDataset(Vector<Record> dataRecord) {
        DefaultCategoryDataset result1 = new DefaultCategoryDataset();
        DefaultCategoryDataset result2 = new DefaultCategoryDataset();
        String series1 = "Query Time";
        String series2 = "Exact Candidates";
        String series3 = "Similarity Candidates";

        for(int i = 0; i< dataRecord.size(); i++)
        {
         Record r = dataRecord.elementAt(i);
         String type = "Step "+(i+1);
         result1.addValue(r.getTime(), series1, type);
         result2.addValue(r.getExaSize(), series2, type);
         result2.addValue(r.getSimSize(), series3, type);
        }

      return  createChart(result1, result2);

    }


    /**
     * Creates a chart.
     *
     * @return A chart.
     */
    private static JFreeChart createChart( CategoryDataset dataset1, CategoryDataset dataset2) {

        NumberAxis rangeAxis1 = new NumberAxis("Response Time (ms)");
        rangeAxis1.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        LineAndShapeRenderer renderer1 = new LineAndShapeRenderer();
        renderer1.setBaseToolTipGenerator(
                new StandardCategoryToolTipGenerator());
        CategoryPlot subplot1 = new CategoryPlot(dataset1, null, rangeAxis1,
                renderer1);
        subplot1.setDomainGridlinesVisible(true);

        NumberAxis rangeAxis2 = new NumberAxis("Candidates");
        rangeAxis2.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        BarRenderer renderer2 = new BarRenderer();
        renderer2.setBaseToolTipGenerator(
                new StandardCategoryToolTipGenerator());
        CategoryPlot subplot2 = new CategoryPlot(dataset2, null, rangeAxis2,
                renderer2);
        subplot2.setDomainGridlinesVisible(true);

        CategoryAxis domainAxis = new CategoryAxis("Query Formulation Steps");
        CombinedCategoryPlot plot = new CombinedCategoryPlot(
                domainAxis, new NumberAxis("Range"));
        
        plot.add(subplot1, 2,rangeAxis1);
        plot.add(subplot2, 1,rangeAxis2);

        JFreeChart result = new JFreeChart("",new Font("SansSerif", Font.BOLD, 12), plot, true);

        return result;

    }

    /**
     * Creates a panel for the demo (used by SuperDemo.java).
     *
     * @return A panel.
     */
    public static JPanel createDemoPanel(Vector<Record> dataRecord) {
        JFreeChart chart = createDataset(dataRecord);
        return new ChartPanel(chart);
    }

  
}
